/*
	created time:2018/4/2
	author:Chason
	describe:初始化游戏
*/
class Game extends egret.Sprite{
	private constructor() {
		super();
		this.initialGame();
		//监听添加到显示列表
		this.addEventListener(egret.Event.ADDED_TO_STAGE,()=>{
           this.start();
        }, this);	
	}
	
	//舞台宽度高度
	private  stageWidth=egret.MainContext.instance.stage.stageWidth;
	private  stageHeight=egret.MainContext.instance.stage.stageHeight;
	


	/*
		经典游戏按钮
	*/
	public classicBtn;
	/*
		残局闯关按钮
	*/ 
	public passGameBtn;

	  //容器大小和锚点还有位置的参数
	public  btnWrapOption={
		w:250,
		h:162,
		ax:125,
		ay:81,
		x:this.stageWidth-200,
		y:200
	}	
	private initClassic(){
		let option=this.btnWrapOption;
		this.classicBtn=new eui.Button();
		this.classicBtn.skinName="classicBtn";
		this.classicBtn.y=option.y;
		this.classicBtn.x=option.x;
		this.classicBtn.anchorOffsetX=option.ax;
		this.classicBtn.anchorOffsetY =option.ay;
		  //添加按钮事件
		Basic.touchScaleBtn(this.classicBtn);		                                                                                                                                                                                                                                     
		  //点击事件--跳转到残局游戏
		   this.classicBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{         
			 alert("开发中...敬请期待");
		 },this);
		  
		  return this.classicBtn
	 }

	 /*
	 	残局闯关按钮
	 */
	 private passGameBtnFun():eui.Button{
		 let option=this.btnWrapOption;
		 this.passGameBtn= new eui.Button();
		 this.passGameBtn.skinName="passBtn";
		 this.passGameBtn.x=option.x-30;
		 this.passGameBtn.y=option.y+180;
		 this.passGameBtn.anchorOffsetX=option.ax;
		 this.passGameBtn.anchorOffsetY =option.ay;
		  //添加按钮事件
		 Basic.touchScaleBtn(this.passGameBtn);		                                                                                                                                                                                                                                     
		  //点击事件--跳转到残局游戏
		   this.passGameBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{         
			  Scene.main.removeChild(this);
              Scene.passGame = new PassGame();
              Scene.main.addChild(Scene.passGame);
		 },this);

		 return this.passGameBtn;
	 }





	
	
	 //右侧按钮动画
	 public btnAnimation(wrap){
		 let btnTw=egret.Tween.get(wrap);
		  btnTw.to({x:this.btnWrapOption.x+30})
		     .to({x:this.btnWrapOption.x-30},500,egret.Ease.backOut);
	 }

	/*
	    该场景添加到显示列表执行
	*/
    public start():void{
		//背景音乐
        Music.musicPlay("welcome_mp3");
		//按钮动画
		this.btnAnimation(this.classicBtn);
		this.btnAnimation(this.passGameBtn);
	}

	/*
		初始化游戏
	*/
	private initialGame():void{	
		  //游戏背景
		  let gameBg:egret.Bitmap = new egret.Bitmap();
          gameBg.texture=RES.getRes("gameBg_jpg");
          gameBg.x=0;
          gameBg.y=0;
          gameBg.width=this.stageWidth;
          gameBg.height=this.stageHeight;
          this.addChild(gameBg);
		  //经典游戏按钮
		  let classicBtn=this.initClassic();
		  this.addChild(classicBtn);
		  //残局闯关游戏
		  let passGameBtn=this.passGameBtnFun();
		  this.addChild(passGameBtn);
		  //顶部容器
		  let header=Header.getInstance();
		  this.addChild(header);
		  //人物
		  let person=Boy.getInstance();
		  this.addChild(person);		  
	}
	

	//单例模式
	public static game:Game;
	public static getInstance():Game{
		if(!this.game){
			this.game=new Game();
		}			
		return this.game;
	}
}